﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleConfigurator.Model;
using VehicleConfigurator.Database;
using log4net;


namespace VehicleConfigurator
{
    public partial class FrmCurrency : Form
    {
        static readonly ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); //Constructing log

        public FrmCurrency()
        {
            InitializeComponent();
        }

        private void FrmCurrency_Load(object sender, EventArgs e)
        {
            this.LoadData();
            this.ReFormatGrid();
        }
       

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.UpdateData();
                //DialogResult result1 = MessageBox.Show("Are you sure to save the change?", "Confirm Message", MessageBoxButtons.YesNo);
                //if (result1 == DialogResult.Yes)
                //{

                //    this.UpdateData();
                //}
            }
            catch (Exception ex)
            {
                
                log.Error(ex.Message);
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw ex;
            }
        }
        

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = (DataTable)dgvCurrency.DataSource;
            Currency cc = new Currency(-1);
            dt.Rows.Add(cc.OID, cc.Code, cc.Name, cc.IsLocal, cc.IsDefault);
            dgvCurrency.DataSource = dt;
        }
        public void LoadData()
        {
            string sql = "SELECT	oid,CODE,NAME,CASE IsLocal WHEN 1 THEN TRUE WHEN 0 THEN	FALSE END AS IsLocal, CASE IsDefault WHEN 1 THEN TRUE WHEN 0 THEN FALSE END AS IsDefault FROM currency";
            clsDb db = new clsDb();
            db.LoadData(sql, this.dgvCurrency);
        }
        public void UpdateData()
        {
            clsDb db = new clsDb();
            db.updateData("currency", this.dgvCurrency);
            this.LoadData();
        }
        void ReFormatGrid()
        {
            dgvCurrency.AllowUserToAddRows = false;
            dgvCurrency.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvCurrency.ScrollBars = ScrollBars.Both;
        }

        private void dgvCurrency_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //MessageBox.Show(dgvCountryCode.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
            if (dgvCurrency.Columns[e.ColumnIndex].Name == "isDefault"
                && dgvCurrency.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "0")
            {
                //MessageBox.Show(dgvCountryCode.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
                foreach (DataGridViewRow r in dgvCurrency.Rows)
                {
                    if (r.Cells["isDefault"].Value.ToString().Equals("1") && r.Index != e.RowIndex)
                    {
                        r.Cells["isDefault"].Value = false;
                    }
                }
            }
            if (dgvCurrency.Columns[e.ColumnIndex].Name == "isLocal"
                && dgvCurrency.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "0")
            {
                //MessageBox.Show(dgvCountryCode.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
                foreach (DataGridViewRow r in dgvCurrency.Rows)
                {
                    if (r.Cells["isLocal"].Value.ToString().Equals("1") && r.Index != e.RowIndex)
                    {
                        r.Cells["isLocal"].Value = false;
                    }
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow item in this.dgvCurrency.SelectedRows)
            {
                dgvCurrency.Rows.RemoveAt(item.Index);

            }
        }

    }
}
